package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.NewsService;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.NewsServiceImpl;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 一个页面对于一个servlet  一个对象对于一个service和一个model和一个dao
 */
@WebServlet("/product/findAll")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.取得前端的值
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String cuisineId=req.getParameter("cuisineId");
//        3.封装model

        ProductModel productModel = new ProductModel();
        productModel.setWhereShow(3);
        productModel.setPage(Tool.strToPage(page));
        productModel.setLimit(Tool.strToProductLimit(limit));
        productModel.setCuisineId(Tool.strToInt(cuisineId));
//        4.调用服务逻辑层
        ProductService produceService = new ProductServiceImpl();
//        5.得到数据
        List<ProductModel> productModelList = produceService.getProductModelList(productModel);

        //        (5)用JSONObject合并多个对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productModelList", productModelList);
//        6.返回数据
        resp.getWriter().println(jsonObject);
    }
}
